package cz.uhk.max.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

public class DateUtils {
	
	public static String date2String(Date date, String pattern) {
		if (date == null) return "";
		DateFormat format = new SimpleDateFormat(pattern, Locale.getDefault());
		return format.format(date);
	}
	
	public static String date2String(Date date, String pattern, Locale locale) {
		if (date == null) return "";
		DateFormat format = new SimpleDateFormat(pattern, locale);
		return format.format(date);
	}
	
	public static Date string2Date(String string, String pattern) throws ParseException {
		if (string == null && string.equals("")) return null;
		DateFormat format = new SimpleDateFormat(pattern, Locale.getDefault());
		return format.parse(string);
	}
	
	public static Date string2Date(String string, String pattern, Locale locale) throws ParseException {
		if (string == null && string.equals("")) return null;
		DateFormat format = new SimpleDateFormat(pattern, locale);
		return format.parse(string);
	}
	
	public static Date createDate(int day, int month, int year) {
		return createDate(0, 0, day, month, year);
	}
	
	public static String now (){
		Calendar now = Calendar.getInstance();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd_hh_mm_ss");
        return formatter.format(now.getTime()).toString();

	}
	
	public static Date createDate(int minute, int hour, int day, int month, int year) {
		Calendar calendar = GregorianCalendar.getInstance();
		calendar.set(Calendar.MILLISECOND, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MINUTE, minute);
		calendar.set(Calendar.HOUR_OF_DAY, hour);
		calendar.set(Calendar.DAY_OF_MONTH, day);
		calendar.set(Calendar.MONTH, month);
		calendar.set(Calendar.YEAR, year);
		return calendar.getTime();
	}
	
	public static Date addToDate(Date date, int field, int value) {
		Calendar calendar = GregorianCalendar.getInstance();
		calendar.setTime(date);
		calendar.add(field, value);
		return calendar.getTime();
	}
	
	public static String sqlDateBezUvozovek(Date dat) {
		String s;
		SimpleDateFormat formatSql = new SimpleDateFormat("yyyyMMdd");
		 s = formatSql.format(dat);
		return s;
	}
}
